package com.sls.web.manager.game.service.impl;

import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.NutsNet;
import com.sls.web.manager.game.pojo.NutsNetOrder;
import com.sls.web.manager.game.querybean.NutsNetOrderQuery;
import com.sls.web.manager.game.querybean.NutsNetQuery;
import com.sls.web.manager.game.service.NutsNetService;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.socket.SuperService;

@Repository("nutsNetService")
public class NutsNetServiceImpl extends SuperService implements NutsNetService {
	private static Logger log = Logger.getLogger(NutsNetServiceImpl.class);
	private Gson gson;

	public NutsNetServiceImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public boolean confirmAudit(String suppliersId, int priceRatio,
			UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.NUTS_NET_AUDIT);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		NutsNet nutsNet = new NutsNet();
		nutsNet.setSuppliersId(suppliersId);
		nutsNet.setPriceRatio(priceRatio);
		nutsNet.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, nutsNet);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
			return true;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public Pagination<NutsNet> queryNutsNet(NutsNetQuery nutsNetQuery,
			UserInfoVO userInfoVO) throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter NutsNetService queryNutsNet method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.NUTS_NET_PAGE_QUERY);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, nutsNetQuery);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
			log.info("服务端返回的信息为：" + dto.getBody());
			String body = dto.getBody();
			Pagination<NutsNet> nutsNet = gson.fromJson(body,
					new TypeToken<Pagination<NutsNet>>() {
					}.getType());
			log.info("当前用户：" + userInfoVO.getUserId()
					+ "，Leave NutsNetService queryNutsNet method...");

			return nutsNet;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public NutsNet queryNutsNetById(String id, UserInfoVO userInfoVO)
			throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter NutsNetService queryNutsNetById method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.NUTS_NET_QUERY_BY_ID);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		NutsNet nutsNet = new NutsNet();
		nutsNet.setSupProductId(id);
		SocketDTO dto = super.send(head, nutsNet);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
			log.info("服务端返回的信息为：" + dto.getBody());
			NutsNet nutsNet2 = gson.fromJson(dto.getBody(), NutsNet.class);
			log.info("当前用户：" + userInfoVO.getUserId()
					+ "，Leave GoodsBrandSerice queryGoodsBrandById method...");

			return nutsNet2;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public List<NutsNet> querySupUserId(NutsNet nutsNet, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.QUERY_SUPUSERTID_LIST);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head);
		if (dto != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			List<NutsNet> nutsNet2 = gson.fromJson(dto.getBody(),
					new TypeToken<List<NutsNet>>() {
					}.getType());
			return nutsNet2;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public NutsNet queryBalance(NutsNetQuery nutsNetQuery, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.NUTS_NET_BALANCE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, nutsNetQuery);
		if (dto != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body) && !body.equals("\"\"")) {
				NutsNet nutsNet2 = gson.fromJson(dto.getBody(), NutsNet.class);
				return nutsNet2;
			} else {
				throw new ManagerException("未知错误！");
			}
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public NutsNetOrder queryOrder(NutsNetOrderQuery nutsNetOrderQuery,
			UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.NUTS_NET_QUERY_ORDER);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, nutsNetOrderQuery);
		if (dto != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body) && !body.equals("\"\"")) {
				NutsNetOrder order2 = gson.fromJson(body, NutsNetOrder.class);
				return order2;
			} else {
				throw new ManagerException("未知错误！");
			}
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public NutsNetOrder queryOrderById(String ptOrderNo, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		NutsNetOrder order = new NutsNetOrder();
		order.setPtOrderNo(ptOrderNo);
		SocketDTO dto = super.send(head, order);
		if (dto != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			NutsNetOrder order2 = gson.fromJson(dto.getBody(),
					NutsNetOrder.class);
			return order2;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public void goodsDetailSync(UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.NUTS_NET_GOODS_DETAIL);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public void batchOnline(String goodsState, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.NUTS_NET_BATCH_ON_OFF_LINE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		NutsNet nutsnet = new NutsNet();
		nutsnet.setGoodsState(goodsState);
		nutsnet.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, nutsnet);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public void batchOffline(String goodsState, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.NUTS_NET_BATCH_ON_OFF_LINE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		NutsNet nutsnet = new NutsNet();
		nutsnet.setGoodsState(goodsState);
		nutsnet.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, nutsnet);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

}
